#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :文件打包_按照文件夹数量.py
# @Time      :2024/3/11 
# @Author    :CL
# @email     :1037654919@qq.com
import os
import shutil
import zipfile


def batch_zip_folders(input_folder, batch_size):
    folders = [name for name in os.listdir(input_folder) if os.path.isdir(os.path.join(input_folder, name))]
    num_batches = len(folders) // batch_size + 1
    folders.sort()  # 按照文件夹名称排序
    print(f'folders: {folders},num_batches: {num_batches}')
    for i in range(num_batches):
        start_idx = i * batch_size
        end_idx = min((i + 1) * batch_size, len(folders))

        batch_folders = folders[start_idx:end_idx]

        with zipfile.ZipFile(f'{input_folder.replace("pinterest","pinterest_zip")}pinterest{i + 1}.zip', 'w') as zipf:
            print(f'Zipping batch {i + 1}...')
            for folder in batch_folders:
                folder_path = os.path.join(input_folder, folder)
                for root, _, files in os.walk(folder_path):
                    for file in files:
                        file_path = os.path.join(root, file)
                        zipf.write(file_path, arcname=os.path.relpath(file_path, input_folder))
if __name__ == "__main__":
    input_folder = "/media/chenglei3/77D014CED257D1411/images/pinterest/"
    batch_size = 17  # 每个批次包含的文件夹数量
    # 打包
    batch_zip_folders(input_folder, batch_size)
